package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.XiaoXiXiangQingBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.XiaoXiTongJiAdapter;
import com.oscar.ehome.view.pageview.view.XiaoXiXiangQingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/12/7
 */

public class xiaoxiqingActivity extends BasesActivity<XiaoXiXiangQingView, PagePresenter> implements XiaoXiXiangQingView, XRefreshView.XRefreshViewListener,
        AdapterView.OnItemClickListener, ProgressFrameLayout.AgainRequestListener {
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.message_listview)
    MyList messageListview;
    @BindView(R.id.message_refview)
    XRefreshView messageRefview;
    @BindView(R.id.mess_fragment)
    ProgressFrameLayout messFragment;
    XiaoXiTongJiAdapter adapter;
    String token = "";
    private int page = 1;//页数
    private PagePresenter pagePresenter;
    List<XiaoXiXiangQingBean.liebiaoshuju> list = new ArrayList<>();
    public static long lastRefreshTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_tongjilist);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(xiaoxiqingActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("消息统计");
        token = SystemConfigFactory.getInstance(xiaoxiqingActivity.this).getSystemConfig().getToken();

        titleImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        messageListview.setOnItemClickListener(this);
        messFragment.setAgainRequestListener(this);
        messageRefview.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        messageRefview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        messageRefview.setPullLoadEnable(false);
        // 设置上次刷新的时间
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        messageRefview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        messageRefview.setAutoRefresh(false);

    }

    /**
     * 加载数据
     */
    public void initData() {
        showLoading();
        if (isNetWork()) {
            messFragment.stopRefresh();
            messageListview.setVisibility(View.VISIBLE);
            pagePresenter.xiaoxitongjixiangqing(token,"20",page+"");
        } else {
            messFragment.setVisibility(View.VISIBLE);
            messFragment.setLoadFail();
            messageListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent in = new Intent();
        in.setClass(this, xiaoxifinalActivity.class);

        in.putExtra("id", list.get(position));
        startActivity(in);

    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    @Override
    public void getxiaoxilistMessage(XiaoXiXiangQingBean result) {
        dismissLoading();
        messageRefview.stopRefresh();
        if (result != null && !result.equals("")) {
            if (result.getList().size() > 0) {

                messFragment.stopRefresh();
                messageListview.setVisibility(View.VISIBLE);
                list = result.getList();
                adapter = new XiaoXiTongJiAdapter(this, list);
                messageListview.setAdapter(adapter);

            } else {
                messageListview.setVisibility(View.GONE);
                messFragment.setNoResult();
                dismissLoading();
            }
            if (result.getList().size() < 20) {
                messageRefview.stopLoadMore();
            }
        } else {
            dismissLoading();
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        messageRefview.stopRefresh();
        ToastUtil.show(this, getString(R.string.erry));
        messFragment.setLoadFail();
        messageListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        messageRefview.stopRefresh();
        ToastUtil.show(this, result);
        if (result.contains("过期")) {
            this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            this.finish();
        } else {
            messFragment.setLoadFail();
            messageListview.setVisibility(View.GONE);
        }
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }


    @Override
    public void againRequest() {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = messageRefview.getLastRefreshTime();
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        pagePresenter.xiaoxitongjixiangqing(token ,"20", s);
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);
        pagePresenter.xiaoxitongjixiangqing(token ,"20", s);
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
